.PHONY: all clean crt kernel drivers library manager tests generate

TOOLSDIR     := tools
TOOLSROOTDIR := root

PROJECT_ROOT := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
export PATH  := $(PROJECT_ROOT)/$(TOOLSDIR)/$(TOOLSROOTDIR)/bin:$(PATH)

GENERATED_SOURCES := sdk/syscall_enum.h kernel/src/syscall_table.inl library/src/wrappers.c

all: livecd.iso tests

crt:
	$(MAKE) -C crt all

kernel: generate crt
	$(MAKE) -C kernel all

drivers: kernel
	$(MAKE) -C drivers all

library: generate crt
	$(MAKE) -C library all

manager: library
	$(MAKE) -C manager all

tests:
	$(MAKE) -C tests all

generate: $(GENERATED_SOURCES)

sdk/syscall_enum.h: $(wildcard sdk/*.h) syscalls.awk
	@echo 'Generating system call sources/headers...'
	sort sdk/* | awk -v enum_file=sdk/syscall_enum.h -v table_file=kernel/src/syscall_table.inl -v wrappers_file=library/src/wrappers.c -f syscalls.awk

kernel/src/syscall_table.inl: sdk/syscall_enum.h

library/src/wrappers.c: sdk/syscall_enum.h

livecd.iso : kernel drivers library manager
	mkdir -p livecd/monolithium livecd/boot/grub livecd/monolithium/drivers
	cp kernel/monolithium livecd/monolithium/kernel
	find drivers -type f -name '*.drv' -exec cp {} livecd/monolithium/drivers/ \;
	cp manager/manager livecd/monolithium/manager
	cp grub.cfg livecd/boot/grub/grub.cfg
	grub-mkrescue -o $@ livecd

clean:
	$(MAKE) -C crt clean
	$(MAKE) -C kernel clean
	$(MAKE) -C drivers clean
	$(MAKE) -C library clean
	$(MAKE) -C manager clean
	$(MAKE) -C tests clean
	rm -f $(GENERATED_SOURCES) livecd.iso
